package net.common.db;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * connection 连接管理器
 * 
 * @author 孙树林
 * 
 */
public class ConnectionManager {

	private static ConnectionManager connectionManager = null; // 连接管理器

	private static ConnectionPool connectionPool = null; // 连接池
	
	private static ThreadLocal<Connection> local = new ThreadLocal<Connection>();

	/**
	 * 构造函数
	 * 
	 */
	private ConnectionManager() {
		connectionPool = ConnectionPool.getInstance();
	}

	/**
	 * 连接管理器实例
	 * 
	 * @return
	 */
	public static void init() {
		if (connectionManager == null) {
			connectionManager = new ConnectionManager();
		}
	}

	/**
	 * 连接管理器实例
	 * 
	 * @return
	 */
	public static ConnectionManager getInstance() {
		if (connectionManager == null) {
			connectionManager = new ConnectionManager();
		}
		return connectionManager;
	}

	/**
	 * 连接池
	 * 
	 * @return
	 */
	public static ConnectionPool getConnectionPool() {
		return connectionPool;
	}

	/**
	 * 获得数据库连接
	 * 
	 * @return
	 * @throws SQLException
	 */
	public static Connection getConnection() throws SQLException {
		Connection conn = local.get();
		if(conn == null) {
			conn = getConnection(false);
			local.set(conn);
		}
		return conn;
	}
	
	public static void destoryConnection() throws Exception {
		try{
			Connection conn = local.get();
			local.remove();
			if(conn != null) {
				destroyConnection(conn);
			}
		}catch(SQLException sqle){
			throw new Exception(sqle);
		}
	}
	
	public static void releaseConnection() throws Exception {
		try{
			Connection conn = local.get();
			local.remove();
			if(conn != null) {
				releaseConnection(conn);
			}
		}catch(SQLException sqle){
			throw new Exception(sqle);
		}
	}

	/**
	 * 获得日志数据库连接
	 * 
	 * @return
	 * @throws SQLException
	 */
	public static Connection getConnection(boolean isLog) throws SQLException {
		return connectionPool.getConnection(isLog);
	}

	/**
	 * 释放数据库连接
	 * 
	 * @param conn
	 * @throws SQLException
	 */
	public static void releaseConnection(Connection conn) throws SQLException {
		connectionPool.releaseConnection(conn);
	}

	/**
	 * 释放日志数据库
	 * 
	 * @param conn
	 * @param bDebug
	 * @throws SQLException
	 */
	public static void releaseConnection(Connection conn, boolean bDebug)
			throws SQLException {
		connectionPool.releaseConnection(conn, true, bDebug);
	}

	/**
	 * 销毁数据库连接
	 * 
	 * @param conn
	 * @throws SQLException
	 */
	public static void destroyConnection(Connection conn) throws SQLException {
		connectionPool.destroyConnection(conn);
	}

	/**
	 * 销毁日志数据库连接
	 * 
	 * @param conn
	 * @param bDebug
	 * @throws SQLException
	 */
	public static void destroyConnection(Connection conn, boolean bDebug)
			throws SQLException {
		connectionPool.destroyConnection(conn, bDebug);
	}

	/**
	 * 销毁所有连接
	 * 
	 */
	public static void destroyAll() {
		connectionPool.destroyAll();
	}
}